﻿Imports System.Data.OleDb

Public Class WebForm1
    Inherits System.Web.UI.Page
    Dim maxNo As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Check if the Page has a cakeNo attached and if not redirect to list
        If Request.Params("cakeNo") = Nothing Then
            'No CakeNo stated so redircet
            Response.Redirect("list.aspx")
        Else

            'Check if Number exists if not redirect to opps
            Dim sql8 As String = "Select * FROM Cakes WHERE ID = @f1"
            Dim conn8 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
            Dim cmd8 As OleDbCommand = New OleDbCommand(sql8, conn8)
            cmd8.Parameters.AddWithValue("@f1", Request.Params("cakeNo"))
            conn8.Open()
            Dim dr8 As OleDbDataReader = cmd8.ExecuteReader()
            dr8.Read()
            If dr8.HasRows Then
                'Record Exists so do not add in as Hit
            Else
                'No Cake
                Response.Redirect("opps.aspx?id=13")
            End If
            dr8.Close()
            conn8.Close()
            cmd8.Dispose()
            conn8.Dispose()
        End If

        'Record the IP Address of the vistor and record the cake have having a HIT if the IP address is unquire within 24 hours
        'Get The IP Address
        Dim userIP As String
        Dim userHN As String = System.Net.Dns.GetHostName
        userIP = System.Net.Dns.GetHostAddresses(userHN).GetValue(0).ToString()

        'Get Todays Date
        Dim todaysDate As DateTime = New DateTime(Now.Year, Now.Month, Now.Day)


        'SQL
        Dim SQL As String = "SELECT * FROM CakePop WHERE C_ID = @f1 AND pageHitIP = @f2 AND PageHitDate = @f3"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
        cmd.Parameters.AddWithValue("@f1", Request.Params("cakeNo"))
        cmd.Parameters.AddWithValue("@f2", userIP)
        cmd.Parameters.AddWithValue("@f3", todaysDate)
        conn.Open()

        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        dr.Read()

        If dr.HasRows Then
            'Record Exists so do not add in as Hit
        Else
            'No Record so record as Hit

            Dim SQL2 As String = "INSERT INTO CakePop (C_ID,pageHitIP,PageHitDate) VALUES (@f1,@f2,@f3)"
            Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
            Dim cmd2 As OleDbCommand = New OleDbCommand(SQL2, conn2)
            cmd2.Parameters.AddWithValue("@f1", Request.Params("cakeNo"))
            cmd2.Parameters.AddWithValue("@f2", userIP)
            cmd2.Parameters.AddWithValue("@f3", todaysDate)
            conn2.Open()
            cmd2.ExecuteNonQuery()

            conn2.Close()
            cmd2.Dispose()
            conn2.Dispose()

        End If

        dr.Close()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Dim cake As Integer = 1
        If Request.Params("cakeNo") = Nothing Then
            'Do Nothing
        Else
            cake = Request.Params("cakeNo")
        End If

        Dim Sqltag As String = "SELECT MAX(CountOfCT_FID) FROM TagCount WHERE C_FID = @f1"
        Dim conntag As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmdtag As OleDbCommand = New OleDbCommand(Sqltag, conntag)
        cmdtag.Parameters.AddWithValue("@f1", cake)
        conntag.Open()

        maxNo = cmdtag.ExecuteScalar()

        conntag.Close()
        cmdtag.Dispose()
        conntag.Dispose()

        'Find and record the users rating
        If User.Identity.IsAuthenticated Then
            If IsPostBack Then
                Class1.rateTheCake(Class1.getUserNumber(User.Identity.Name), Request.Params("cakeNo"), cakeRate.SelectedValue)
            Else
                If Class1.ratedCake(Class1.getUserNumber(User.Identity.Name), Request.Params("cakeNo")) > 0 Then
                    cakeRate.SelectedValue = Class1.ratedCake(Class1.getUserNumber(User.Identity.Name), Request.Params("cakeNo"))
                End If
            End If

        Else
            cakeRate.Visible = False
        End If



    End Sub

    Function getTags(ByVal tagCount As Integer, ByVal tag As String)

        Dim myoutput As String = "<DIV CLASS STYLE=font-size:"
        Dim myresult As Double
        myresult = (tagCount / maxNo) * 100
        myresult = myresult * 0.5
        Dim roundresult As Integer
        roundresult = Convert.ToInt32(myresult)
        If maxNo = 1 Then
            'Only one tag each so 12px
            myoutput = myoutput & "12px;float:left;>" & "<a href=cakeByTags.aspx?tag=" & tag & ">" & tag & "</a> </div>"
        Else
            myoutput = myoutput & roundresult & "px;float:left;>" & "<a href=cakeByTags.aspx?tag=" & tag & ">" & tag & "</a> </div>"
        End If


        Return myoutput
    End Function

    Function createlink()
        Return "<A href=cakeTag.aspx?cakeId=" & Request.Params("cakeNo") & ">Tag This Cake </a>"
    End Function

    Function createlink2()
        Return "<A href=commentOnCake.aspx?cakeId=" & Request.Params("cakeNo") & ">Add your Comment </a>"
    End Function


    Protected Sub cakeRate_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cakeRate.SelectedIndexChanged
        'Rate the Cake
        Class1.rateTheCake(Class1.getUserNumber(User.Identity.Name), Request.Params("cakeNo"), cakeRate.SelectedValue)
    End Sub
End Class